package tmall.action;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;

import tmall.pojo.Category;
import tmall.pojo.Product;
import tmall.util.Page;

public class ProductAction extends Action4Result{
	
	
	@Action("admin_product_list")
	public String list(){
		if(page == null)
			page = new Page();
		int total = productService.total(category);
		page.setTotal(total);
		page.setParam("&category.id"+category.getId());
		products = productService.list(page, category);
		for(Product product : products){
			productImageService.setFirstProductImage(product);
		}
		category = (Category) categoryService.get(category.getId());
		return "listProduct";
	}
	
	@Action("admin_product_add")
	public String add(){
		product.setCreateDate(new Date());
		productService.save(product);
		return "listProductPage";
	}
	
	@Action("admin_product_delete")
	public String delete(){
		product = (Product) productService.get(product.getId());
		productService.delete(product);
		return "listProductPage";
	}
	
	@Action("admin_product_edit")
	public String edit(){
		product = (Product) productService.get(product.getId());
		return "editProduct";
	}
	
	
	@Action("admin_product_update")
	public String update(){
		Product productFormDB = (Product)productService.get(product.getId());
		product.setCreateDate(productFormDB.getCreateDate());
		productService.update(product);
		return "listProductPage";
	}
}
